package com.zyk.controller;


import com.zyk.entity.Resource;
import com.zyk.entity.User;
import com.zyk.service.IResourceService;
import com.zyk.vo.FinalResult;
import com.zyk.vo.ResourceAllTreeVo;
import com.zyk.vo.ResourcetreeGridVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 资源 前端控制器
 * </p>
 *
 * @author zyk
 * @since 2017-11-20
 */
@Controller
@RequestMapping("resource")
public class ResourceController {

    @Autowired
    private IResourceService resourceService;


    /**
     * 跳转到资源管理页面
     */
    @RequestMapping("manager")
    public String manager() {
        return "admin/resource/resource";
    }

    /**
     * 跳转到添加资源的页面
     */
    @RequestMapping("addPage")
    public String addPage() {
        return "admin/resource/resourceAdd";
    }

    /**
     * 跳转到编辑资源的页面
     */
    @RequestMapping("editPage")
    public String editPage(Resource resource, Model model) {
        model.addAttribute("resource", resourceService.selectById(resource.getId()));
        return "admin/resource/resourceEdit";
    }


    /**
     * 资源管理列表
     * treeGrid
     *
     * @return
     */
    @RequestMapping("treeGrid")
    @ResponseBody
    public List<ResourcetreeGridVo> treeGird() {
        return resourceService.treeGird();
    }


    /**
     * 资源树，查询所有的菜单，即resource_type=0的资源
     * allTree
     */
    @RequestMapping("allTree")
    @ResponseBody
    public Set<ResourceAllTreeVo> allTree() {
        return resourceService.allTree();
    }

    @RequestMapping("allTrees")
    @ResponseBody
    public Set<ResourceAllTreeVo> allTrees() {
        return resourceService.allTrees();
    }

    /**
     * 资源树，根据登录用户的uid获取对应的资源树
     * tree
     */
    @RequestMapping("tree")
    @ResponseBody
    public Set<ResourceAllTreeVo> tree() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        return resourceService.tree(user.getId());
    }

    @RequestMapping("trees")
    @ResponseBody
    public Set<ResourceAllTreeVo> trees() {
        return resourceService.trees();
    }


    @RequestMapping("add")
    @ResponseBody
    public FinalResult add(Resource resource) {
        FinalResult add = resourceService.add(resource);
        return add;
    }

    @RequestMapping("delete")
    @ResponseBody
    public FinalResult delete(Resource resource) {
        return resourceService.delete(resource);
    }

    @ResponseBody
    @RequestMapping("edit")
    public FinalResult edit(Resource resource){
        resource.setCreateTime(new Date());
        if(resourceService.updateAllColumnById(resource)){
            return new FinalResult("编辑成功", true);
        } else {
            return new FinalResult("编辑失败", false);
        }
    }
}
